﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNS_BLL;
using QLNS_Public;

namespace QLNS_GiaoDien
{
    public partial class uc_PHONGBAN : UserControl
    {
        public uc_PHONGBAN()
        {
            InitializeComponent();
        }
        PhongBan_BLL phongban_bll = new PhongBan_BLL();
        PhongBan_Public pb = new PhongBan_Public();
        private void uc_PHONGBAN_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = phongban_bll.LayDuLieu();
        }
        int flag;
        private void btn_Them_Click(object sender, EventArgs e)
        {
            flag = 0;
        }

        private void btn_Sua_Click(object sender, EventArgs e)
        {
            flag = 1;
        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            try
            {
                pb.MaPhong = Convert.ToInt32(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Mã Phòng"));
                pb.TenPhong = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Tên Phòng Ban").ToString();
                if (MessageBox.Show("Bạn có muốn xóa Phòng Ban: '" + pb.MaPhong + "' có Mã là: '" + pb.TenPhong + "' này không?", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    phongban_bll.Delete_PhongBan(pb);
                    uc_PHONGBAN_Load(sender, e);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Không thể xóa do có bản ghi phụ thuộc", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                pb.TenPhong = txt_tenphong.Text;
                pb.DienThoai = txt_Dienthoai.Text;
                pb.GhiChu = txt_ghichu.Text;
                phongban_bll.Insert_PhongBan(pb,pb,pb);
                MessageBox.Show("Thêm mới thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            if (flag == 1)
            {
                pb.MaPhong = int.Parse(txt_MaPhong.Text);
                pb.TenPhong = txt_tenphong.Text;
                pb.DienThoai = txt_Dienthoai.Text;
                pb.GhiChu = txt_ghichu.Text;
                phongban_bll.Update_PhongBan(pb,pb, pb, pb);
                MessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            uc_PHONGBAN_Load(sender,e);
        }

        private void txt_MaPhong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsControl(e.KeyChar) && !Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }
    }
}
